﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockCommon.data;
using StockImport.dataimport.otc.dailyindex;
using StockImport.dataimport.tse.dailyindex;
using StockImport.dataimport.tse.sb;
using StockImport.dataimport.otc.sb;
using StockCommon;

namespace CommonUI
{
    public partial class ImportTaskInvokeControl : UserControl
    {
        public ImportTaskInvokeControl()
        {
            InitializeComponent();
        }
        protected int getBackTraceCount()
        {
            return Convert.ToInt32(numericUpDown1.Value);
        }

        public void refreshDailySBState()
        {
            DateTime dt = StockDailyIndexServices.getLastTradeDate();
            List<DateTime> tds = StockDailyIndexServices.getTradeDays(dt, 90);
            var context=LinqAgent.getContext();
            Dictionary<DateTime,SBDetailsLog> tse= (from ls in context.SBDetailsLogs where ls.市場 == "tse" select ls).ToDictionary(x => x.日期);
            Dictionary<DateTime, SBDetailsLog> otc = (from ls in context.SBDetailsLogs where ls.市場 == "otc" select ls).ToDictionary(x => x.日期);

            List<StockDailyRecCount> result = new List<StockDailyRecCount>();
            foreach (DateTime date in tds)
            {
                StockDailyRecCount rec = new StockDailyRecCount();
                result.Add(rec);
                rec.日期 = date.ToShortDateString();
                if (tse.ContainsKey(date))
                {
                    rec.上市筆數=tse[date].股票家數;
                }
                if (otc.ContainsKey(date))
                {
                    rec.上櫃筆數=otc[date].股票家數;
                }
                
            }
            dataGridView2.DataSource = result;
        }
        public void refreshDailyIndexState()
        {
            DateTime dt = StockDailyIndexServices.getLastTradeDate();
            List<DateTime> tds = StockDailyIndexServices.getTradeDays(dt, 90);
            List<StockDailyRecCount> result = new List<StockDailyRecCount>();
            foreach (DateTime date in tds)
            {
                int c1 = StockDailyIndexServices.getRecordCount(date, TwnStockMarketType.上市);
                int c2 = StockDailyIndexServices.getRecordCount(date, TwnStockMarketType.上櫃);

                StockDailyRecCount rec = new StockDailyRecCount();
                rec.日期 = date.ToShortDateString();
                rec.上市筆數 = c1;
                rec.上櫃筆數 = c2;
                result.Add(rec);
            }
            dataGridView1.DataSource = result;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            refreshDailyIndexState();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            refreshDailyIndexState();
        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point pt = dataGridView1.PointToScreen(e.Location);
                contextMenuStrip1.Show(pt);
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
               
        }

        protected DateTime? getSelectedDate()
        {
            if(dataGridView1.SelectedRows.Count==0)
                return null;
            String v = (String)dataGridView1.SelectedRows[0].Cells[0].Value;
            
            DateTime dt=new DateTime();
            if (DateTime.TryParse(v, out dt))
                return dt;
            return null;
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DateTime? dt = getSelectedDate();
            if (dt != null)
            {
                TSE_DailyIndexImport imp = new TSE_DailyIndexImport();
                imp.update((DateTime)dt, true);
            }
            refreshDailyIndexState();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            DateTime? dt = getSelectedDate();
            if (dt != null)
            {
                OTC_DailyIndexImport imp = new OTC_DailyIndexImport();
                imp.update((DateTime) dt, true);
            }
            refreshDailyIndexState();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            refreshDailySBState();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            refreshDailyIndexState();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            TSE_SB_Fetcher fetcher = new TSE_SB_Fetcher();
            fetcher.Update();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OTC_SB_Fetcher o = new OTC_SB_Fetcher();
            o.Update();
        }
    }
    public class StockDailyRecCount
    {
        public String 日期 { get; set; }
        public int 上市筆數 { get; set; }
        public int 上櫃筆數 { get; set; }
    }
}
